<?php

namespace App\Admin\Resources;

/**
 * @mixin \App\Admin\Models\AdminRole
 */
class AdminRoleResource extends JsonResource
{
    public const FOR_INDEX = 'index';
    public const FOR_EDIT = 'edit';
    public const FOR_SHOW = 'show';

    public function toArray($request)
    {
        $data = [
            'permissions' => $this->mergePermissions()
        ];
        return array_merge(parent::toArray($request),$data);
    }

    protected function mergePermissions()
    {
        return [
            $this->mergeFor(static::FOR_INDEX, function () {
                return $this->permissions()->withTrashed(isRestore())->pluck('name');
            }),
            $this->mergeFor(static::FOR_EDIT, function () {
                return $this->permissions()->withTrashed(isRestore())->select(['id', 'name'])->get();
            }),
            $this->mergeFor(static::FOR_SHOW, function () {
                return $this->permissions()->withTrashed(isRestore())->pluck('name');
            }),
        ];
    }

}
